<?php

namespace App\Http\Controllers\Ajax;

use App\Common\Constant\Status;
use App\Models\Master;
use App\Services\ICookieService;
use App\Services\IMasterService;
use App\Services\ISessionService;
use App\Services\IMobileService;
use Illuminate\Http\Request;
use Validator;

class AuthController extends BaseController
{

    /**
     * 短信验证
     * @param Request $request
     */
    public function smsVerify(Request $request,IMobileService $mobileService,ICookieService $cookieService){
        /**
         * 检查验证码正确与否
         */
        if ($request->isMethod('post') && $request->has('verifyCode') && $request->has('phoneNum')){
            $validator = Validator::make($request->all(), [
                'phoneNum' => 'required|regex:/^1[34578][0-9]{9}$/',
                'verifyCode' => 'required',
            ],[
                'phoneNum.regex'=>'请正确填写手机号码'
            ]);
            if ($validator->fails()){
                return $this->renderJson(Status::FAIL,'',$validator->errors()->first());
            }

            $ret = $mobileService->smsVerify($request->input('phoneNum'),$request->input('verifyCode'));
            if ($ret){
                return $this->renderJson($cookieService->verifiedPhone($request->input('phoneNum')) ? Status::SUCCESS : Status::FAIL);
            }else{
                return $this->renderJson(Status::MOBILE_SMS_VERIFY_WRONG);
            }
        }
        /**
         * 发送验证码
         */
        else if ($request->isMethod('post') && $request->has('phoneNum')){
            $validator = Validator::make($request->all(), [
                'phoneNum' => 'required|regex:/^1[34578][0-9]{9}$/',
            ],[
                'phoneNum.regex'=>'请正确填写手机号码'
            ]);
            if ($validator->fails()){
                return $this->renderJson(Status::FAIL,'',$validator->errors()->first());
            }

            return $this->renderJson($mobileService->smsVerify($request->input('phoneNum')) ? Status::SUCCESS : Status::FAIL);
        }

        return $this->renderJson(Status::ERROR);
    }

    /**
     * 注册
     * @param Request $request
     */
    public function register(Request $request,ISessionService $sessionService,IMasterService $masterService,ICookieService $cookieService){
        if ($request->isMethod('post') && $request->has('phoneNum')){
            $validator = Validator::make($request->all(), [
                'phoneNum' => 'required|regex:/^1[34578][0-9]{9}$/|unique:'.Master::table().',phone',
            ],[
                'phoneNum.regex'=>'请正确填写手机号码',
                'phoneNum.unique'=>'这个手机号已存在'
            ]);
            if ($validator->fails()){
                return $this->renderJson(Status::FAIL,'',$validator->errors()->first());
            }

            $user = $masterService->add(
                $request->weixinUserId,$request->phoneNum,$request->realName,$request->idcardNum,$request->areaId,
                $request->serviceAreaIds,$request->serviceCategory,$request->serviceTypes,$request->idcardFront,
                $request->idcardBack,$request->idcardHold);
            if ($user){
                if (!$sessionService->setCurrentUser($user)){
                    return $this->renderJson(Status::SESSION_SETTING_ERROR);
                }
                if (!$cookieService->currentUserid($user->id)){
                    return $this->renderJson(Status::COOKIE_SETTING_ERROR);
                }
                return $this->renderJson(Status::SUCCESS);
            }
        }
        return $this->renderJson(Status::ERROR);
    }
}
